package com.gitee.wsl.transform.validator

import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

class ValidatorProperty<T>(defaultValue:T,val validator: Validator<T,*>) : ReadWriteProperty<Any, T> {

    private var _value = defaultValue

    override fun getValue(thisRef: Any, property: KProperty<*>): T {
        return  _value
    }

    override fun setValue(thisRef: Any, property: KProperty<*>, value: T) {
        if(value.validateUse(validator))
            _value = value
    }
}

fun <T> validate(defaultValue: T,rules: Validator<T,*>) = ValidatorProperty(defaultValue,rules)